package com.lb.shopguide.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import com.blankj.utilcode.util.ToastUtils;
import com.lb.shopguide.R;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.base.BaseActivity;
import com.lb.shopguide.ui.fragment.FragmentChooseRole;
import com.lb.shopguide.ui.fragment.FragmentGuide;
import com.umeng.analytics.MobclickAgent;

import java.util.Timer;
import java.util.TimerTask;

import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * Created by kang on 2017/5/26.
 */

public class GuideActivity extends BaseActivity {
    private boolean isExit = false;
    private UserConfigManager mUserConfigManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        mUserConfigManager = UserConfigManager.getInstance(this);
        if (!isTaskRoot()) {//home键点击后，回到application，会重新开启此页有bug
            finish();
            return;
        }
        if (getIntent().hasExtra(AppConstant.INTENT_KEY_FROM_LOGOUT)) {
            loadRootFragment(R.id.fl_container, FragmentChooseRole.newInstance(null));
        } else {
            loadRootFragment(R.id.fl_container, FragmentGuide.newInstance());
        }
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return super.onCreateFragmentAnimator();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (getTopFragment() instanceof FragmentGuide) {

                }
//                else if (getTopFragment() instanceof FragmentEnterPhone && ((FragmentEnterPhone) getTopFragment()).isVerify()) {
//                    ((FragmentEnterPhone) getTopFragment()).backToEnterPhone();
//                }
                else {
                    if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
                        pop();
                    } else {
                        exitBy2Click();
                    }
                }
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    //以后放到别的地方,双击退出android应用
    private void exitBy2Click() {
        Timer tExit = null;
        if (!isExit) {
            isExit = true; // 准备退出
            ToastUtils.showShort("再按一次退出程序");
            tExit = new Timer();
            tExit.schedule(new TimerTask() {
                @Override
                public void run() {
                    isExit = false; // 取消退出
                }
            }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
        } else {
            finish();
            System.exit(0);

        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);       //统计时长
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public Intent getGuideMainActivityIntent() {
        Intent intent = new Intent(this, GuideMainActivity.class);
        if (getMainActivityBundle() != null) {
            intent.putExtras(getMainActivityBundle());
        }
        return intent;
    }

    public Intent getBossMainActivityIntent() {
        Intent intent = new Intent(this, BossMainActivity.class);
        if (getMainActivityBundle() != null) {
            intent.putExtras(getMainActivityBundle());
        }
        return intent;
    }

    private Bundle getMainActivityBundle() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle != null && bundle.containsKey(AppConstant.PUSH_TYPE)) {
            return bundle;
        } else {
            return null;
        }
    }
}
